﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Properties;
using EFong.View.Proxy;
using EFong.Contract;

namespace EFong.View.Login
{
    public partial class Frm_Login : BasicForm
    {
        public Frm_Login()
        {
            InitializeComponent();
        }

        protected override void Events()
        {
            this.btn_ChangePwd.Click += (s, e) => { this.changePassword(); };
            this.btn_Login.Click += (s, e) => { this.login(); };
            this.btn_Exit.Click += (s, e) => { this.Close(); };
            this.txt_Server.Validated += (s, e) => { ProxyMgt.IP = txt_Server.GetText(); };
            this.txt_Port.Validated += (s, e) => { ProxyMgt.Port = txt_Port.GetText(); };
        }

        protected override void LoadDatas()
        {
            lbl_Caption.Text = AppParameters.SystemName;
            this.txt_Server.SetText(Settings.Default.ServerIP);
            this.txt_Port.SetText(Settings.Default.Port);
            this.txt_User.SetText(Settings.Default.LoginUserName);

            ProxyMgt.IP = txt_Server.GetText();
            ProxyMgt.Port = txt_Port.GetText();
        }

        protected override void OnShown(EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.txt_Server.GetText()))
                txt_User.Focus();
            if (!string.IsNullOrEmpty(this.txt_User.GetText()))
                txt_Pwd.Focus();
        }

        private void login()
        {
            UIOperManager.SaveData(
                () =>
                {
                    if (string.IsNullOrEmpty(txt_User.GetText()))
                    {
                        MsgBox.Info("请输入用户名.");
                        return false;
                    }

                    return true;
                },
                () =>
                {
                    var _baseProxy = BaseServiceProxy.GetInstance();
                    ServiceResult<Contract.User> result = _baseProxy.Login(txt_User.GetText(), txt_Pwd.GetText());

                    AppParameters.LoginUser = result.ReturnValue;

                    return result.IsSuccess;
                },
                string.Empty,
                () =>
                {
                    Settings.Default.ServerIP = txt_Server.GetText();
                    Settings.Default.Port = txt_Port.GetText();
                    Settings.Default.LoginUserName = txt_User.GetText();
                    Settings.Default.Save();
                    DialogResult = DialogResult.OK;
                    this.Close();
                });
        }

        private void changePassword()
        {
            Frm_ChangePassword frm = new Frm_ChangePassword();
            this.Hide();
            frm.UserName = txt_User.GetText();
            frm.Password = txt_Pwd.GetText();
            frm.ShowDialog();
            this.Show();
        }
    }
}
